<html>
  <script>
    myWindowId = 0;
    chrome.browserAction.onClicked.addListener(function() {
      if (myWindowId == 0) {
        chrome.windows.create({
          url: 'https://home.live.com/Handlers/WebIM.mvc',
          type: 'popup',
          width: 700
        }, function(window) {
          myWindowId = window.id;
          chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
            if (tab.windowId == myWindowId && changeInfo.status == 'loading') {
              if (tab.url.indexOf('https://home.live.com/Handlers/WebIM.mvc') != -1) {
                if (JSON.parse(localStorage.removeAd)) {
                  chrome.tabs.executeScript(tabId, {
                    code: 'wim_l.removeChild(wim_wad);c_mm_cl3.style.pixelHeight+=60;wim_wcl.style.pixelHeight+=60'
                  });
                }
                chrome.tabs.executeScript(tabId, {
                  file: 'contentscript.js'
                });
                chrome.windows.update(myWindowId, {
                  width: 700
                });
              } else if (tab.url.indexOf('https://login.live.com/login.srf') != -1) {
                chrome.windows.update(myWindowId, {
                  width: 400
                });
              }
            }
          });
        });
      } else {
        chrome.windows.update(myWindowId, {
          focused: true
        });
      }
    });
    chrome.windows.onRemoved.addListener(function(windowId) {
      if (windowId == myWindowId) {
        myWindowId = 0;
      }
    });
    chrome.extension.onConnect.addListener(function(port) {
      console.assert(port.name == 'WebIM');
      port.onMessage.addListener(function(msg) {
        if (JSON.parse(localStorage.messageSound)) {
          messageSound.load();
          messageSound.play();
        }
        if (JSON.parse(localStorage.messageAlert)) {
          var notification = webkitNotifications.createNotification(msg.img, msg.name, msg.text);
          notification.show();
          setTimeout(function() {
            notification.cancel();
          }, 5000);
        }
      });
      console.log('WebIM connected');
    });
    if (localStorage.volume == null) {
      localStorage.volume = 0.3;
    }
    if (localStorage.messageSound == null) {
      localStorage.messageSound = true;
    }
    if (localStorage.removeAd == null) {
      localStorage.removeAd = false;
    }
    if (localStorage.messageAlert == null) {
      localStorage.messageAlert = true;
    }
    messageSound = document.createElement('audio');
    messageSound.setAttribute('src', 'type.mp3');
    messageSound.play();
    messageSound.pause();
    messageSound.volume = localStorage.volume;
  </script>

</html>